#region copyright
/*
* Copyright (c) 2009, Dion Kurczek
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY DION KURCZEK ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL DION KURCZEK BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using SCG.General;

namespace SCG.Prism
{
    //Represents server statistics
    public class PrismServerStats
    {
        //Constructor
        public PrismServerStats(DateTime startTimeUT, long bytesRead, long bytesWritten,
            int connections, int totalConnections, int peakConnections, int rooms,
            int totalRooms, int peakRooms)
        {
            _startTime = startTimeUT;
            _bytesRead = bytesRead;
            _bytesWritten = bytesWritten;
            _connections = connections;
            _totalConnections = totalConnections;
            _peakConnections = peakConnections;
            _rooms = rooms;
            _totalRooms = totalRooms;
            _peakRooms = peakRooms;
        }
        public PrismServerStats(string s)
        {
            Tokenizer tok = new Tokenizer(s);
            _startTime = tok.TokenDateTime;
            _connections = tok.TokenInt;
            _bytesRead = tok.TokenInt;
            _bytesWritten = tok.TokenInt;
            _totalConnections = tok.TokenInt;
            _peakConnections = tok.TokenInt;
            _rooms = tok.TokenInt;
            _totalRooms = tok.TokenInt;
            _peakRooms = tok.TokenInt;
        }

        //Public members
        public DateTime StartTimeUT
        {
            get
            {
                return _startTime;
            }
        }
        public long BytesRead
        {
            get
            {
                return _bytesRead;
            }
        }
        public long BytesWritten
        {
            get
            {
                return _bytesWritten;
            }
        }
        public int Connections
        {
            get
            {
                return _connections;
            }
        }
        public int TotalConnections
        {
            get
            {
                return _totalConnections;
            }
        }
        public int PeakConnections
        {
            get
            {
                return _peakConnections;
            }
        }
        public int Rooms
        {
            get
            {
                return _rooms;
            }
        }
        public int TotalRooms
        {
            get
            {
                return _totalRooms;
            }
        }
        public int PeakRooms
        {
            get
            {
                return _peakRooms;
            }
        }
        
        //Public methods
        public override string ToString()
        {
            Tokenizer tok = new Tokenizer();
            return tok.Tokenize(StartTimeUT, Connections, BytesRead, BytesWritten, TotalConnections,
                PeakConnections, Rooms, TotalRooms, PeakRooms);
        }

        //Time running friendly
        public string TimeRunning
        {
            get
            {
                TimeSpan tsActive = DateTime.Now - StartTimeUT.ToLocalTime();
                string s = "";
                int d = (int)tsActive.TotalDays;
                if (d > 0)
                    s = Pluralize(d, "day");
                if (tsActive.Hours > 0 || s != "")
                    s += " " + Pluralize(tsActive.Hours, "hr");
                if (tsActive.Minutes > 0 || s != "")
                    s += " " + Pluralize(tsActive.Minutes, "min");
                s += " " + Pluralize(tsActive.Seconds, "sec");
                return s;
            }
        }

        //Bandwidth friendly
        public string BandwidthIn
        {
            get
            {
                return FormatByteCount(BytesRead);
            }
        }
        public string BandwidthOut
        {
            get
            {
                return FormatByteCount(BytesWritten);
            }
        }

        //Private members
        private DateTime _startTime;
        private long _bytesRead;
        private long _bytesWritten;
        private int _connections;
        private int _totalConnections;
        private int _peakConnections;
        private int _rooms;
        private int _totalRooms;
        private int _peakRooms;

        //Make displayable version of byte count
        private string FormatByteCount(long p)
        {
            string suffix = " bytes";
            double n = p;
            if (n >= 1024.0d)
            {
                n /= 1024.0d;
                suffix = " Kb";
            }
            if (n >= 1024.0d)
            {
                n /= 1024.0d;
                suffix = " Mb";
            }
            if (n >= 1024.0d)
            {
                n /= 1024.0d;
                suffix = " Gb";
            }
            return n.ToString("N2") + suffix;
        }

        //Return correct plural form of value
        private string Pluralize(int n, string s)
        {
            string Plural = n + " " + s;
            if (n != 1)
                Plural += "s";
            return Plural;
        }
    }
}
